package com.example.randomname;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import com.example.randomnamemodule.RandomNameGenerator;

public class MainActivity extends AppCompatActivity {
    // 四个名单文件的文件名
    private static final String LIST1_FILE = "list_243.txt";
    private static final String LIST2_FILE = "list_244.txt";
    private static final String LIST3_FILE = "list_245.txt";
    private static final String LIST4_FILE = "list_246.txt";

    // 控件
    private RadioGroup rgNameLists;
    private TextView tvCurrentList;
    private Button btnRandomName;
    private TextView tvResult;

    private int randomCount;

    // 随机点名工具类
    private RandomNameGenerator nameGenerator;
    // 当前选中的文件名
    private String currentFileName = LIST1_FILE;

    @SuppressLint("NonConstantResourceId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化控件
        initViews();

        tvCurrentList.setText("当前名单: " + currentFileName);
        randomCount = 0;
        // 初始化点名工具
        nameGenerator = new RandomNameGenerator();
        // 加载默认名单
        nameGenerator.loadNameListFromAssets(this, currentFileName);
        final int list1ID = R.id.rb_list1;
        final int list2ID = R.id.rb_list2;
        final int list3ID = R.id.rb_list3;
        final int list4ID = R.id.rb_list4;
        // 监听RadioGroup选择变化
        rgNameLists.setOnCheckedChangeListener((group, checkedId) -> {
            // 根据选中的RadioButton切换名单
            if(checkedId == list1ID) {
                currentFileName = LIST1_FILE;
            } else if (checkedId == list2ID) {
                currentFileName = LIST2_FILE;
            } else if (checkedId == list3ID) {
                currentFileName = LIST3_FILE;
            } else if (checkedId == list4ID) {
                currentFileName = LIST4_FILE;
            }
            randomCount = 0;
            tvCurrentList.setText("当前名单: " + currentFileName);
            // 加载选中的名单
            nameGenerator.loadNameListFromAssets(MainActivity.this, currentFileName);
        });

        // 点名按钮点击事件
        btnRandomName.setOnClickListener(v -> {
            // 从当前名单随机抽取名字
            String randomName = nameGenerator.getRandomName();
            randomCount++;
            tvResult.setText("本次点名第" + randomCount + "个：" +randomName);
        });
    }

    /**
     * 初始化所有控件
     */
    private void initViews() {
        rgNameLists = findViewById(R.id.rg_name_lists);
        tvCurrentList = findViewById(R.id.tv_current_list);
        btnRandomName = findViewById(R.id.btn_random_name);
        tvResult = findViewById(R.id.tv_result);

        // 设置默认选中的RadioButton
        ((RadioButton) findViewById(R.id.rb_list1)).setChecked(true);
    }
}
